<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.weifly.jettycn.domain.Catalog" %>
<%@ page import="com.weifly.jettycn.util.TreeNode" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>添加catalog</title>
    <%@include file="/comhead.jsp" %>
    <script type="text/javascript">

    // 插入catalog事件
	function insertCatalogHandler(){
		var msgs = [];
		var name = $("#catalog_name").attr("value");
		if(Tas.isEmpty(name)){
			msgs.push("名称不能为空");
		}
		if(msgs.length>0){
			Tas.showError(msgs);
		}else{
			Tas.hideMessageArea();
			$("#catalogForm").ajaxSubmit(function(response, statusText){
				response = $.parseJSON(response);
				if(response.success){
					window.location.assign("catalogList.do");
				}else{
					Tas.showError(response.messages);
				}
			});
		}
	}

    // 更新子catalog事件
	function changeLevelHandler(){
		var pc = $(this).attr("value");
		if(pc=="-1"){
			updateSubCatalog([]);
		}else{
			$.ajax({
				url: "loadSubCatalog.do",
				data: {
					catalogid: pc
				},
				success: function(data, textStatus){
					data = $.parseJSON(data);
					if(data.success){
						updateSubCatalog(data.param);
					}else{
						Tas.showError(data.messages);
					}
				}
			});
		}
	}

    // 更新level2 select
	function updateSubCatalog(subList){
		var level2Select = $("#catalog_level2");
		// 删除option
		level2Select.children(".sub_opt").remove();
		// 添加option
		for(var i=0;i<subList.length;i++){
			var sub = subList[i];
			var str = "<option value=\"" + sub.id + "\" class=\"sub_opt\">" + sub.name + "</option>";
			level2Select.append(str);
		}
		level2Select.attr("value", "-1");
	}
    
    $(document).ready(function(){
		$("#catalog_submit").click(insertCatalogHandler);
		$("#catalog_level1").change(changeLevelHandler);
    });
    </script>
  </head>
  <body>
    <h3>添加项目类别</h3>
    <%@include file="/message.jsp" %>
    <form id="catalogForm" action="insertCatalog.do" method="post">
    <table class="table_show1" border="1px" bordercolor="#ccbbaa">
      <tr>
        <td>名称：</td>
        <td>
          <input id="catalog_name" name="catalog_name" style="width: 240px;">
        </td>
      </tr>
      <tr>
        <td>父类别：</td>
        <td>
          <select id="catalog_level1" name="catalog_level1" style="width: 120px;">
            <option value="-1" default>=空=</option>
<%
	List catalogList = (List)request.getAttribute("catalogList");
	for(int i=0;i<catalogList.size();i++){
		TreeNode node = (TreeNode)catalogList.get(i);
		Catalog cata = (Catalog)node.getTarget();
%>
            <option value="<%=cata.getCatalogid()%>"><%=cata.getName()%></option>
<%
	}
%>
          </select>
          <select id="catalog_level2" name="catalog_level2" style="width: 120px;">
            <option value="-1" default>=空=</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>叶子节点：</td>
        <td>
          <select id="catalog_leaf" name="catalog_leaf" style="width: 120px;">
            <option value="true" default>是</option>
            <option value="false">否</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>显示顺序：</td>
        <td>
          <input id="catalog_order" name="catalog_order" style="width: 240px;">
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <button id="catalog_submit" type="button">确定</button>
        </td>
      </tr>
    </table>
    </form>
  </body>
</html>